package cn.bgs.mq.MqConfig;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/9/27-19:47
 *  用延时插件下单付款
 */

@Configuration
public class ShopQueueConfig {

    //队列
    public static final String DELAYED_QUEUE_NAME="shop.queue";
    //交换机
    public static final String DELAYED_EXCHANGE_NAME="shop.exchange";
    //routingKing
    public static final String DELAYED_ROUTING_KEY="shop.rotingkey";


    //死信队列
    public static final String DEAD_LETTER_QUEUE="shop.dead.queue";
    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE="shop.dead.exchange";
    //routingKing
    public static final String DEAD_ROUTING_KEY="shop.dead.rotingkey";


    //邮件队列
    public static final String EMAIL_QUEUE_NAME="email.queue";
    //邮件交换机
    public static final String EMAIL_EXCHANGE_NAME="email.exchange";
    //routingKing
    public static final String EMAIL_ROUTING_KEY="email.rotingkey";

    //声明邮件队列
    @Bean
    public Queue emailQueue(){
        //绑定死信交换机
        HashMap<String,Object> hashMap = new HashMap<>(2);

        hashMap.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        hashMap.put("x-dead-letter-routing-key",DEAD_ROUTING_KEY);

        return new Queue(EMAIL_QUEUE_NAME,true,false,false,hashMap);
    }


    //声明队列
    @Bean
    public Queue shopQueue(){
        return new Queue(DELAYED_QUEUE_NAME);
    }
    //声明死信队列
    @Bean("shopDead")
    public Queue shopDead(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    //声明交换机
    @Bean
    public CustomExchange delayedExchange(){
        HashMap<String , Object> hashMap = new HashMap<>();
        hashMap.put("x-delayed-type","direct");

        return new CustomExchange(DELAYED_EXCHANGE_NAME,"x-delayed-message",true,false,hashMap);
    }

    //邮件交换机
    @Bean
    public TopicExchange emailExchange(){
        return new TopicExchange(EMAIL_EXCHANGE_NAME);
    }


    //死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }


    //绑定交换机和队列
    @Bean
    public Binding delBindingDel(@Qualifier("shopQueue") Queue shopQueue, @Qualifier("delayedExchange") CustomExchange delayedExchange){

        return BindingBuilder.bind(shopQueue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();

    }

    //绑定邮件交换机和队列
    @Bean
    public Binding emailBindingDel(@Qualifier("emailQueue") Queue emailQueue, @Qualifier("emailExchange") TopicExchange emailExchange){

        return BindingBuilder.bind(emailQueue).to(emailExchange).with(EMAIL_ROUTING_KEY);

    }
    //死信交换机和死信队列
    @Bean
    public Binding deadBindingDel(@Qualifier("shopDead") Queue shopDead, @Qualifier("deadExchange") DirectExchange deadExchange){

        return BindingBuilder.bind(shopDead).to(deadExchange).with(DEAD_ROUTING_KEY);
    }
}
